home *** CD-ROM | disk | FTP | other *** search
- /*
- * Threads - TransSkel Thread Manager demonstration application
- *
- * This is a very simple-minded demonstration. It puts up a single
- * window that draws a horizontal line, a vertical line, and an oval
- * whose horizontal and vertical axes are the same length as the current
- * lengths of the horizontal and vertical lines. Each of the lines
- * and the oval are drawn independently, using three threads. In order
- * to emphasize the independent nature of the three threads, the
- * graphical object draw by each updates at a different rate.
- *
- * 23 Jul 95 Version 1.00, Paul DuBois
- */
-
- # include "TransSkel.h"
-
- # include <Threads.h>
-
-
- /* window positioning ratios */
-
- # define hRatio FixRatio (1, 2)
- # define vRatio FixRatio (1, 5)
-
- /*
- * Resource numbers
- */
-
- # define aboutAlrtRes 1000 /* About box */
- # define errAlrtRes 1001 /* error alert */
- # define fileMenuNum (skelAppleMenuID + 1) /* File menu */
-
- /* error string resource numbers */
-
- # define noThreadManager 1000
- # define noThread 1001
-
-
- # define windowTitle "\pThreadDemo"
- # define windowWidth 300
- # define windowHeight 120
-
- # define maxLineLen 100
-
- /* thread update times */
-
- # define thread1Update 1L
- # define thread2Update 3L
- # define thread3Update 2L
-
- /* file menu item numbers */
-
- enum
- {
- quit = 1
- };
-
-
- static MenuHandle fileMenu;
-
- static short length1 = 0; /* horizontal line length */
- static short length2 = 0; /* vertical line length */
-
-
- static void
- Die (short strNum)
- {
- StringHandle h;
-
- h = GetString (strNum);
- if (h != (StringHandle) nil)
- {
- HLock ((Handle) h);
- ParamText (*h, nil, nil, nil);
- HUnlock ((Handle) h);
- }
- else
- ParamText ("\pAn unknown error occurred.", nil, nil, nil);
-
- (void) SkelAlert (errAlrtRes, SkelDlogFilter (nil, true),
- skelPositionOnParentDevice);
- SkelRmveDlogFilter ();
- SkelCleanup ();
- ExitToShell ();
- }
-
-
- /* -------------------------------------------------------------------- */
- /* Menu handling procedures */
- /* -------------------------------------------------------------------- */
-
-
- /*
- * Handle selection of "About Hello..." item from Apple menu
- */
-
- static pascal void
- DoAppleMenu (short item)
- {
- (void) SkelAlert (aboutAlrtRes, SkelDlogFilter (nil, true),
- skelPositionOnParentDevice);
- SkelRmveDlogFilter ();
- }
-
-
- /*
- * Process selection from File menu.
- */
-
- static pascal void
- DoFileMenu (short item)
- {
- switch (item)
- {
- case quit:
- SkelStopEventLoop ();
- break;
- }
- }
-
-
- /*
- * Initialize menus. Tell TransSkel to process the Apple menu
- * automatically, and associate the proper procedure with the
- * File menu.
- *
- * \311 is the ellipsis character.
- */
-
- static void
- SetupMenus (void)
- {
- SkelApple ("\pAbout ThreadDemo\311", DoAppleMenu);
- fileMenu = NewMenu (fileMenuNum, "\pFile");
- AppendMenu (fileMenu, "\pQuit/Q");
- (void) SkelMenu (fileMenu, DoFileMenu, nil, false, false);
-
- DrawMenuBar ();
- }
-
-
- /* -------------------------------------------------------------------- */
- /* Window handling procedures */
- /* -------------------------------------------------------------------- */
-
-
- static pascal void
- Clobber (void)
- {
- WindowPtr w;
-
- GetPort (&w);
- DisposeWindow (w);
-
- /* should really dispose of threads here */
- }
-
-
- /* -------------------------------------------------------------------- */
- /* Thread handling procedures */
- /* -------------------------------------------------------------------- */
-
-
-
- static pascal void *
- DoThread1 (void *threadParam)
- {
- static long refTime = 0L;
- static long delta = 4L;
-
- while (1)
- {
- if (TickCount () >= refTime)
- {
- PenMode (patBic);
- MoveTo (60 - maxLineLen/2, 60);
- LineTo (60 + maxLineLen/2, 60);
- PenNormal ();
- MoveTo (60 - length1/2, 60);
- LineTo (60 + length1/2, 60);
- if (delta > 0)
- {
- if (length1 >= maxLineLen)
- delta = -delta;
- }
- else
- {
- if (length1 <= 0)
- delta = -delta;
- }
- length1 += delta;
- refTime = TickCount() + thread1Update;
- }
- (void) YieldToAnyThread ();
- }
- return (nil); /* will never be reached */
- }
-
-
- static pascal void *
- DoThread2 (void *threadParam)
- {
- static long refTime = 0L;
- static long delta = 4L;
-
- while (1)
- {
- if (TickCount () >= refTime)
- {
- PenMode (patBic);
- MoveTo (150, 60 - maxLineLen/2);
- LineTo (150, 60 + maxLineLen/2);
- PenNormal ();
- MoveTo (150, 60 - length2/2);
- LineTo (150, 60 + length2/2);
- if (delta > 0)
- {
- if (length2 >= maxLineLen)
- delta = -delta;
- }
- else
- {
- if (length2 <= 0)
- delta = -delta;
- }
- length2 += delta;
- refTime = TickCount() + thread2Update;
- }
- (void) YieldToAnyThread ();
- }
- return (nil); /* will never be reached */
- }
-
-
- /*
- * Draw an oval with the dimensions of the horizontal and vertical lines
- * being drawn in threads 1 and 2.
- */
-
- static pascal void *
- DoThread3 (void *threadParam)
- {
- static long refTime = 0L;
- static Rect r = { 0, 0, 0, 0 };
-
- while (1)
- {
- if (TickCount () >= refTime)
- {
- EraseOval (&r);
- SetRect (&r, 0, 0, length1, length2);
- OffsetRect (&r, 240 - length1/2, 60 - length2/2);
- FrameOval (&r);
- refTime = TickCount() + thread3Update;
- }
- (void) YieldToAnyThread ();
- }
- return (nil); /* will never be reached */
- }
-
-
- /*
- * Create window and install handler for it
- */
-
- static void
- WindInit (void)
- {
- WindowPtr w;
- Rect bounds;
- ThreadID dummyID;
-
- SetRect (&bounds, 0, 0, windowWidth, windowHeight);
- if (SkelQuery (skelQHasColorQD))
- {
- w = NewCWindow (nil, &bounds, windowTitle, false,
- noGrowDocProc, (WindowPtr) -1, false, 0L);
- }
- else
- {
- w = NewWindow (nil, &bounds, windowTitle, false,
- noGrowDocProc, (WindowPtr) -1, false, 0L);
- }
- SkelPositionWindow (w, skelPositionOnMainDevice,
- FixRatio (1, 2), FixRatio (1,5));
- (void) SkelWindow (w, nil, nil, nil, nil, nil, Clobber, nil, false);
-
- SelectWindow (w);
- ShowWindow (w);
- SkelDoUpdates ();
- SkelDoEvents (updateMask + activMask);
-
- if (NewThread (kCooperativeThread, DoThread1, nil, 0L,
- kCreateIfNeeded, nil, &dummyID) != noErr)
- {
- Die (noThread);
- }
- if (NewThread (kCooperativeThread, DoThread2, nil, 0L,
- kCreateIfNeeded, nil, &dummyID) != noErr)
- {
- Die (noThread);
- }
- if (NewThread (kCooperativeThread, DoThread3, nil, 0L,
- kCreateIfNeeded, nil, &dummyID) != noErr)
- {
- Die (noThread);
- }
- }
-
-
- /* -------------------------------------------------------------------- */
- /* Main */
- /* -------------------------------------------------------------------- */
-
-
- void
- main (void)
- {
- long time;
-
- SkelInit ((SkelInitParamsPtr) nil); /* initialize */
- if (!SkelQuery (skelQHasThreads))
- Die (noThreadManager);
- SetupMenus (); /* install menu handlers */
- WindInit(); /* install window handler */
- SkelGetWaitTimes (&time, nil); /* set background wait time */
- SkelSetWaitTimes (time, time); /* to same as foreground time */
- SkelSetThreadTimes (1L, 1L);
- SkelEventLoop (); /* loop 'til Quit selected */
- SkelCleanup (); /* clean up */
- }
-